package com.hongxun.project.assets.iPoint.controller;

import java.util.ArrayList;
import java.util.List;

import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.project.assets.iBoxes.domain.IBoxes;
import com.hongxun.project.assets.iBoxes.service.IIBoxesService;
import com.hongxun.project.assets.iCablePit.domain.ICablePit;
import com.hongxun.project.assets.iCablePit.service.IICablePitService;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.iPole.domain.IPole;
import com.hongxun.project.assets.iPole.service.IIPoleService;
import com.hongxun.project.assets.pBoxes.domain.PBoxes;
import com.hongxun.project.assets.pBoxes.service.IPBoxesService;
import com.hongxun.project.assets.pCablePit.domain.PCablePit;
import com.hongxun.project.assets.pCablePit.service.IPCablePitService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 地理走向图 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/iPoint")
public class IPointController extends BaseController
{
    private String prefix = "assets/iPoint";
	
	@Autowired
	private IIPointService iPointService;
	@Autowired
	private IILineService iLineService;
	@Autowired
	private IIPoleService iPoleService;
	@Autowired
	private IBByqService bByqService;
	@Autowired
	private IICablePitService iCablePitService;
	@Autowired
	private IIBoxesService iBoxesService;


	
	@RequiresPermissions("assets:iPoint:view")
	@GetMapping()
	public String iPoint()
	{
	    return prefix + "/iPoint";
	}
	
	/**
	 * 查询地理走向图列表
	 */
	@RequiresPermissions("assets:iPoint:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(IPoint iPoint)
	{
		startPage();
        List<IPoint> list = iPointService.selectIPointList(iPoint);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出地理走向图列表
	 */
	@RequiresPermissions("assets:iPoint:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IPoint iPoint)
    {
    	List<IPoint> list = iPointService.selectIPointList(iPoint);
        ExcelUtil<IPoint> util = new ExcelUtil<IPoint>(IPoint.class);
        return util.exportExcel(list, "iPoint");
    }
	
	/**
	 * 新增地理走向图
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存地理走向图
	 */
	@RequiresPermissions("assets:iPoint:add")
	@Log(title = "地理走向图", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(IPoint iPoint)
	{		
		return toAjax(iPointService.insertIPoint(iPoint));
	}

	/**
	 * 修改地理走向图
	 */
//	@GetMapping("/edit/{id}")
//	public String edit(@PathVariable("id") String id, ModelMap mmap)
//	{
//		IPoint iPoint = iPointService.selectIPointById(id);
//		mmap.put("iPoint", iPoint);
//	    return prefix + "/edit";
//	}

	/**
	 * 同杆点位选择
	 * @return
	 */
	@GetMapping("/choosePole")
	public String choosePole(IPoint iPoint , ModelMap modelMap)
	{
		String poleId= iPoint.getId();
//		String taskId= pPoint.getTaskId();
		String byqId = iPoint.getByqId();
		modelMap.put("Ids",poleId);
		modelMap.put("byqIds",byqId);
		modelMap.put("poleIds",poleId);
//		modelMap.put("taskIds",taskId);
		return prefix + "/choosePole";
	}

	/**
	 *  同杆
	 *
	 * @param iPoint
	 * @return
	 */
	@PostMapping("/listIsPole")
	@ResponseBody
	public TableDataInfo listIsPole(IPoint iPoint)
	{
		startPage();
		String byqId = iPoint.getByqId();
		String id = iPoint.getId();
		String poleId = iPoint.getPoleId();
		List<IPoint> listss = iPointService.selectlistIsPole(byqId,id,poleId);

		List<IPoint> list = new ArrayList<>();
		for (IPoint point : listss) {
			ILine iLine = iLineService.selectILineById(point.getLineId());
			point.setLineName(iLine.getName());
			list.add(point);
		}

		//	分两种情况，①同杆点位有共同的上联点位 ②同杆点位分属关联不同的上联点位
		if(list.size() == 1){
			IPoint iPoint1 = iPointService.selectIPointById(id);
			String upId = iPoint1.getUpId();
			IPoint iPoint2 = new IPoint();
			iPoint2.setUpId(upId);
			List<IPoint> iPoints = iPointService.selectIPointList(iPoint2);
			if(iPoints.size() == 1){
				list.get(0).getPoleId();
//				IPoint iPoint3 = new IPoint();
//				iPoint3.setPoleId();
				List<IPoint> iPoints1 = iPointService.selectlistIsPole(byqId,iPoint1.getPoleId(),iPoint1.getPoleId());
				for (IPoint point : iPoints1) {
					ILine iLine1 = iLineService.selectILineById(point.getLineId());
					point.setLineName(iLine1.getName());
				}
				return getDataTable(iPoints1);
			}else if(iPoints.size() > 1){
				for (IPoint point : iPoints) {
					ILine iLine1 = iLineService.selectILineById(point.getLineId());
					point.setLineName(iLine1.getName());
				}
				return getDataTable(iPoints);
			}
			return getDataTable(iPoints);
		}else if (list.size() > 1){
			return getDataTable(list);
		}

		return getDataTable(list);
	}

	/**
	 * 点位与变压器合并
	 */
	@GetMapping("/editForPointByq/{id}")
	public String editForPointByq(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		IPoint iPoint1 = iPointService.selectIPointById(iPoint.getUpId());
		ILine iLine = iLineService.selectILineById(iPoint.getLineId());
		if(iPoint1 != null)
			iPoint.setUpId(iPoint1.getCode());
		if(iLine != null)
			iPoint.setLineName(iLine.getName());
		else
			iPoint.setLineName("");


		BByq bByq = new BByq();
		bByq.setPointId(id);
		List<BByq> bByqList = bByqService.selectBByqList(bByq);
		BByq byq = new BByq();
		if(bByqList.size() == 0){
			bByq.setPointId(iPoint.getPoleId());
			bByqList = bByqService.selectBByqList(bByq);
			byq = bByqService.selectBByqById(bByqList.get(0).getId());
		}else{
			byq = bByqService.selectBByqById(bByqList.get(0).getId());
		}
		mmap.put("bByq", byq);


		mmap.put("iPoint", iPoint);
		return prefix + "/editForPointByq";
	}

	/**
	 * 点位与下户点合并
	 */
	@GetMapping("/editForPointXhd/{id}")
	public String editForPointXhd(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		IPoint iPoint1 = iPointService.selectIPointById(iPoint.getUpId());
		ILine iLine = iLineService.selectILineById(iPoint.getLineId());
		if(iPoint1 != null)
			iPoint.setUpId(iPoint1.getCode());
		if(iLine != null)
			iPoint.setLineName(iLine.getName());
		else
			iPoint.setLineName("");


		mmap.put("iPoint", iPoint);
		return prefix + "/editForPointXhd";
	}

	/**
	 * 点位与分支箱合并
	 */
	@GetMapping("/editForPointFzx/{id}")
	public String editForPointFzx(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		IPoint iPoint1 = iPointService.selectIPointById(iPoint.getUpId());
		ILine iLine = iLineService.selectILineById(iPoint.getLineId());
		if(iPoint1 != null)
			iPoint.setUpId(iPoint1.getCode());
		if(iLine != null)
			iPoint.setLineName(iLine.getName());
		else
			iPoint.setLineName("");

		mmap.put("iPoint", iPoint);
		return prefix + "/editForPointFzx";
	}

	/**
	 * 点位与电缆井合并
	 */
	@GetMapping("/editForPointDlj/{id}")
	public String editForPointDlj(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		IPoint iPoint1 = iPointService.selectIPointById(iPoint.getUpId());
		ILine iLine = iLineService.selectILineById(iPoint.getLineId());
		if(iPoint1 != null)
			iPoint.setUpId(iPoint1.getCode());
		if(iLine != null)
			iPoint.setLineName(iLine.getName());
		else
			iPoint.setLineName("");


		ICablePit iCablePit = new ICablePit();
		iCablePit.setPId(id);
		List<ICablePit> iCablePits = iCablePitService.selectICablePitList(iCablePit);
		ICablePit iCablePit1 = new ICablePit();
		if(iCablePits.size() == 0){
			iCablePit.setPId(iPoint.getPoleId());
			iCablePits = iCablePitService.selectICablePitList(iCablePit);
			iCablePit1 = iCablePitService.selectICablePitById(iCablePits.get(0).getId());
		}else{
			iCablePit1 = iCablePitService.selectICablePitById(iCablePits.get(0).getId());
		}
		mmap.put("iCablePit", iCablePit1);


		mmap.put("iPoint", iPoint);
		return prefix + "/editForPointDlj";
	}

	/**
	 * 点位与表箱合并
	 */
	@GetMapping("/editForPointBx/{id}")
	public String editForPointBx(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		IPoint iPoint1 = iPointService.selectIPointById(iPoint.getUpId());
		ILine iLine = iLineService.selectILineById(iPoint.getLineId());
		if(iPoint1 != null)
			iPoint.setUpId(iPoint1.getCode());
		if(iLine != null)
			iPoint.setLineName(iLine.getName());
		else
			iPoint.setLineName("");


		IBoxes iBoxes = new IBoxes();
		iBoxes.setPId(id);
		List<IBoxes> iBoxesList = iBoxesService.selectIBoxesList(iBoxes);
		IBoxes iBoxes1 = new IBoxes();
		if(iBoxesList.size() == 0){
			iBoxes.setPId(iPoint.getPoleId());
			iBoxesList = iBoxesService.selectIBoxesList(iBoxes);
			iBoxes1 = iBoxesService.selectIBoxesById(iBoxesList.get(0).getId());
		}else {
			iBoxes1 = iBoxesService.selectIBoxesById(iBoxesList.get(0).getId());
		}


		mmap.put("iBoxes", iBoxes1);


		mmap.put("iPoint", iPoint);
		return prefix + "/editForPointBx";
	}

	/**
	 * 点位与杆塔合并
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		IPoint iPoint1 = iPointService.selectIPointById(iPoint.getUpId());
		ILine iLine = iLineService.selectILineById(iPoint.getLineId());
		if(iPoint1 != null)
			iPoint.setUpId(iPoint1.getCode());
		if(iLine != null)
			iPoint.setLineName(iLine.getName());
		else
			iPoint.setLineName("");


		IPole iPole1 = new IPole();
		iPole1.setPId(id);
		List<IPole> iPoleList = iPoleService.selectIPoleList(iPole1);
		IPole iPole = new IPole();
		if(iPoleList.size() == 0){
			IPoint pPoint = iPointService.selectIPointById(id);
			iPole1.setPId( pPoint.getPoleId());
			iPoleList = iPoleService.selectIPoleList(iPole1);
			iPole = iPoleService.selectIPoleById(iPoleList.get(0).getId());
		}else{
			iPole = iPoleService.selectIPoleById(iPoleList.get(0).getId());
		}
//		IPole iPole = iPoleService.selectIPoleById(id);
		mmap.put("iPole", iPole);


		mmap.put("iPoint", iPoint);
		return prefix + "/edit";
	}

	
	/**
	 * 修改保存地理走向图
	 */
	@RequiresPermissions("assets:iPoint:edit")
	@Log(title = "地理走向图", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(IPoint iPoint)
	{		
		return toAjax(iPointService.updateIPoint(iPoint));
	}
	
	/**
	 * 删除地理走向图
	 */
	@RequiresPermissions("assets:iPoint:remove")
	@Log(title = "地理走向图", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(iPointService.deleteIPointByIds(ids));
	}
	
}
